/*
 * 修订记录：
 * aiping.yuan 12:35 创建
 */
package org.cloud.simple.helloworld;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Strings;


public class DateUtil extends DateUtils {

    public static final String dtSimpleYmd = "yyyyMMdd";

    private static Logger logger = LoggerFactory.getLogger(DateUtil.class);

    private static final String REG_SPANCE = "[ ]";

    /**
     * yyyyMMdd 转换为 date
     * @param strDate
     * @return
     */
    public static Date strDate2ShortDate(String strDate) {
        return strDate2FormatDate(strDate, dtSimpleYmd);
    }

    /**
     * 文本日期转换为指定格式日期
     * @param strDate
     * @param formatDate
     * @return
     */
    public static Date strDate2FormatDate(String strDate, String formatDate) {
        if (strDate == null) {
            return null;
        }
        try {
            return parseDate(strDate, formatDate);
        } catch (ParseException e) {
            logger.warn("strDate={} parse error", strDate);
        }
        return null;
    }

    /**
     * yyyyMMdd 转换为yyyy-MM-dd
     * @param strDate
     * @return
     */
    public static String strDate2ShortStrDate(String strDate) {
        Date date = strDate2ShortDate(strDate);
        return dateToShortStrDate(date);
    }

    /**
     * 日期转 yyyy-MM-dd
     * @param date
     * @return
     */
    public static String dateToShortStrDate(Date date) {
        if (null == date) {
            return null;
        }
        return String.format("%tF", date);
    }

    /**
     * 日期  转换为yyyyMMdd
     * @param date
     * @return
     */
    public static String dateToDTSimpleYmd(Date date) {
        if (date == null) {
            return null;
        }
        return String.format("%tY%<tm%<td", date);
    }

    public static Date getStartTimeOfTheDate(Date date) {
        if (date == null) {
            return null;
        }
        GregorianCalendar now = new GregorianCalendar();
        now.setTime(date);
        now.set(11, 0);
        now.set(12, 0);
        now.set(13, 0);
        now.set(14, 0);
        return now.getTime();
    }

    public static Date getEndTimeOfTheDate(Date date) {
        if (date == null) {
            return null;
        }
        GregorianCalendar now = new GregorianCalendar();
        now.setTime(date);
        now.set(11, 23);
        now.set(12, 59);
        now.set(13, 59);
        now.set(14, 0);
        return now.getTime();
    }

    /**
     * 判定匹配次数
     * @param str
     * @param regex
     * @return
     */
    private static int matcherNum(String str, String regex) {
        int matcherNum = 0;
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str.toLowerCase());
        while (m.find()) {
            matcherNum += 1;
        }
        return matcherNum;
    }

    /**
     * 验证日期格式
     *
     * @param strDate
     *            日期字符串
     * @param pattern
     *            验证格式
     * @return
     */
    public static Boolean isValidDate(String strDate, String pattern) {
        if (Strings.isNullOrEmpty(strDate) || Strings.isNullOrEmpty(pattern)) {
            return false;
        }
        if (strDate.length() != pattern.length()) {
            return false;
        }
        //- 我擦，空格惹的祸
        if (matcherNum(strDate, REG_SPANCE) != matcherNum(pattern, REG_SPANCE)) {
            return false;
        }
        try {
            DateFormat formatter = new SimpleDateFormat(pattern);
            formatter.setLenient(false);
            ParsePosition pos = new ParsePosition(0);
            Date date = formatter.parse(strDate, pos);

            if (date == null || pos.getErrorIndex() > 0) {
                return false;
            }
            if (pos.getIndex() != strDate.length()) {
                return false;
            }
            if (formatter.getCalendar().get(Calendar.YEAR) > 9999) {
                return false;
            }
        } catch (Exception e) {
            return  false;
        }
        return true;
    }

    public static Date getAmountOfTheDate(int amount) {
        GregorianCalendar now = new GregorianCalendar();
        now.add(GregorianCalendar.DAY_OF_MONTH, amount);
        now.set(11, 0);
        now.set(12, 0);
        now.set(13, 0);
        now.set(14, 0);
        return now.getTime();
    }

    /**
     * 判断当前时间是否大于指定时间
     * @param receiveRepayTime
     * @return
     */
    public static boolean isNowTimeAfterRepayTime(String receiveRepayTime) {
        GregorianCalendar now = new GregorianCalendar();
        String strSpecifiedDate = String.format("%tF %s", now.getTime(), receiveRepayTime);
        Date specifiedDate = DateUtil.strDate2FormatDate(strSpecifiedDate, "yyyy-MM-dd HH:mm:ss");
        return now.getTime().after(specifiedDate);
    }

    /**
     * 获取指定年
     * @param amount
     * @return
     */
    public static Date getYearDate(int amount) {
        GregorianCalendar now = new GregorianCalendar();
        now.add(Calendar.YEAR, amount);
        return now.getTime();
    }
}